package requrest

import (
	"errors"
	"github.com/gin-gonic/gin"
	"go-project/dto"
	"go-project/models"
)

type RolesSearchRequest struct {
	Pagination *Pagination
	Name       string `form:"name"`
}
type RolesSInsertRequest struct {
	Name          string `json:"name" binding:"required,roles_name_unique" required_msg:"该角色名称不能为空" roles_name_unique_msg:"该角色名称已存在"`
	PermissionsId string `json:"permissions_id" binding:"required" required_msg:"权限不能为空"`
	Desc          string `json:"desc"`
	Key           string `json:"key" binding:"required,roles_key_unique" required_msg:"该角色标识不能为空" roles_key_unique_msg:"该角色标识已经存在"`
	dto.ValidateBindRequest
}

func (rolesSInsertRequest *RolesSInsertRequest) BindAndValidate(c *gin.Context) error {
	if err := c.ShouldBindJSON(rolesSInsertRequest); err != nil {
		c.Errors = append(c.Errors, c.Error(err).SetType(gin.ErrorTypeBind))
		c.Set("req_dto", rolesSInsertRequest)
		return err
	}
	return nil
}
func (rolesSInsertRequest *RolesSInsertRequest) Generate(data interface{}) *dto.ValidateBindRequest {
	roles, ok := data.(*models.Roles)
	if ok {
		roles.Name = rolesSInsertRequest.Name
		roles.Key = rolesSInsertRequest.Key
		roles.Desc = rolesSInsertRequest.Desc
		return nil
	}
	rolesSInsertRequest.Errors = errors.New("数据类型不正确，数据必须是models.Roles类型")
	return &rolesSInsertRequest.ValidateBindRequest

}

type RolesUpdateRequest struct {
	ID            uint   `json:"id"  binding:"required" required_msg:"id不能为空"`
	PermissionsId string `json:"permissions_id" binding:"required" required_msg:"权限不能为空"`
	Name          string `json:"name" binding:"required,roles_name_unique=update" required_msg:"该角色名称不能为空" roles_name_unique_msg:"该角色名称已存在"`
	Desc          string `json:"desc"`
	Key           string `json:"key" binding:"required,roles_key_unique=update" required_msg:"该角色标识不能为空" roles_key_unique_msg:"该角色标识已经存在"`
	dto.ValidateBindRequest
}

func (rolesUpdateRequest *RolesUpdateRequest) BindAndValidate(c *gin.Context) error {
	if err := c.ShouldBindJSON(rolesUpdateRequest); err != nil {
		c.Errors = append(c.Errors, c.Error(err).SetType(gin.ErrorTypeBind))
		c.Set("req_dto", rolesUpdateRequest)
		return err
	}
	return nil
}
func (rolesUpdateRequest *RolesUpdateRequest) Generate(data interface{}) *dto.ValidateBindRequest {
	roles, ok := data.(*models.Roles)
	if ok {
		roles.ID = rolesUpdateRequest.ID
		roles.Name = rolesUpdateRequest.Name
		roles.Key = rolesUpdateRequest.Key
		roles.Desc = rolesUpdateRequest.Desc
		return nil
	}
	rolesUpdateRequest.Errors = errors.New("数据类型不正确，数据必须是models.Roles")
	return &rolesUpdateRequest.ValidateBindRequest

}

type RolesAuthPerMissionsRequest struct {
	ID            uint   `json:"id"  binding:"required" required_msg:"id不能为空"`
	PermissionsId string `json:"permissions_id" binding:"required" required_msg:"权限不能为空"`
	dto.ValidateBindRequest
}

func (rolesAuthPerMissionsRequest *RolesAuthPerMissionsRequest) BindAndValidate(c *gin.Context) error {
	if err := c.ShouldBindJSON(rolesAuthPerMissionsRequest); err != nil {
		c.Errors = append(c.Errors, c.Error(err).SetType(gin.ErrorTypeBind))
		c.Set("req_dto", rolesAuthPerMissionsRequest)
		return err
	}
	return nil
}
